package com.xpf.shardingtabledemo.shardingdtable.annotion;


import com.xpf.shardingtabledemo.shardingdtable.strategy.DayShardingStrategy.*;
import com.xpf.shardingtabledemo.shardingdtable.strategy.IShardingTableStrategy;
import com.xpf.shardingtabledemo.shardingdtable.strategy.MonthShardingStrategy;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author Lenovo
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NeedShardedTable {
    // 拦截表名
    String tableName();

    // 拦截策略
    ShardedTableStrategy strategy() default ShardedTableStrategy.MONTH;

    public enum ShardedTableStrategy {
        // 按月建表
        MONTH(MonthShardingStrategy.class, TableNumOfMonth.NOTSHARDEDBYDAY),

        // 按天分表
        DAY3(Day3ShardingStrategy.class, TableNumOfMonth.THREE),
        DAY6(Day6ShardingStrategy.class, TableNumOfMonth.SIX),
        DAY10(Day10ShardingStrategy.class, TableNumOfMonth.TEN),
        DAY15(Day15ShardingStrategy.class, TableNumOfMonth.FIFTEEN),
        DAY1(Day1ShardingStrategy.class, TableNumOfMonth.EVERYDAY);

        private IShardingTableStrategy shardingTableStrategy;

        //一个月分的表数
        private TableNumOfMonth tableNumofMonth;

        ShardedTableStrategy(Class<? extends IShardingTableStrategy> shardingTableStrategy, TableNumOfMonth tableNumOfMonth) {
            try {
                this.shardingTableStrategy = shardingTableStrategy.newInstance();
                this.tableNumofMonth = tableNumOfMonth;
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        public IShardingTableStrategy getShardingTableStrategy(){
            return this.shardingTableStrategy;
        }

        public TableNumOfMonth getTableNumOfMonth(){
            return this.tableNumofMonth;
        }

    }

    enum TableNumOfMonth{

        THREE(3),
        SIX(6),
        TEN(10),
        FIFTEEN(15),
        EVERYDAY(1),
        NOTSHARDEDBYDAY(0);

        private int tableNum;

        TableNumOfMonth(int tableNum){
            this.tableNum = tableNum;
        }

        public int getTableNum(){
            return this.tableNum;
        }
    }

}
